### 使用tax_meta_class 时候将分类图片显示到管理栏 
add_filter( 'manage_edit-category_columns', 'add_cat_cover_column' );

function add_cat_cover_column($columns)
{
  $columns['cover'] = esc_html__('COVER','hablog');
  return $columns;
}

add_action( 'manage_category_custom_column', 'cat_cover_custom_column_content',10,3 );
//add_filter('manage_category_custom_column','cat_cover_custom_column_content',1,2);
function cat_cover_custom_column_content($string,$columns,$term_id) {

  if (!empty(get_term_meta( $term_id, 'ha_image_field_id', true ))) {
    $cat_cover = get_term_meta( $term_id, 'ha_image_field_id', true );
  } else {
    $cat_cover = '';
  }
  switch ($columns) {

    case 'cover':
      // code...
      if (!empty($cat_cover)) {
        echo '<img width=50 height=50 src='.$cat_cover['url'].'>';
      } else {
        echo 'No cover found!';
      }

      break;
  }
}


function redirect_to_login() {
	wp_redirect(site_url() . "/login");
	exit;
}

add_action('wp_logout', 'redirect_to_login');

add_action('init', 'fn_redirect_wp_admin');


function fn_redirect_wp_admin() {
	global $pagenow; 
	if ($pagenow == 'wp-login.php' && $_GET['action'] != 'logout') {
		# 增加action 不是 logout ， 因为会引起冲突 ， 会引起多次的跳转， 或者可以删除上面那个钩子跳转
		wp_redirect(home_url('/login'));
		
		exit;
	}
}

//模板首先跳转 
// add_action('template_redirect', 'my_check_login');

// function my_check_login(){
//     if( is_page('login') ){
//         // check input and
//         // do my login stuff here   
// 				wp_redirect('$location', $status)
//     }
// }


//修改默认管理员头像
function change_gravatar_to_local_image( $avatar, $id_or_email, $size, $default, $alt ) {
    $user = false;

    if ( is_numeric( $id_or_email ) ) {
        $id   = (int) $id_or_email;
        $user = get_user_by( 'id', $id );
    } elseif ( is_object( $id_or_email ) ) {
        if ( ! empty( $id_or_email->user_id ) ) {
            $id   = (int) $id_or_email->user_id;
            $user = get_user_by( 'id', $id );
        }
    } else {
        $user = get_user_by( 'email', $id_or_email );
    }

    if ( $user && is_object( $user ) ) {
        $local_image = get_template_directory_uri() . '/images/avatar.jpeg';
        $avatar = preg_replace( "/src='(.*?)'/i", "src='{$local_image}'", $avatar );
        $avatar = preg_replace( "/srcset='(.*?)'/i", "srcset='{$local_image}'", $avatar );
        update_user_meta($user->ID, 'local_avatar', $local_image);
    }
    return $avatar;
}
add_filter( 'get_avatar', 'change_gravatar_to_local_image', 10, 5 );

function author_box() { global $post; ?>

  <div class="post-single-author ">
                                <div class="authors-info">
                                    <div class="image">
                                        <a href="author.html" class="image">
                                            <?php echo get_avatar( get_the_author_meta('ID')); ?>
                                        </a>
                                    </div>
                                    <div class="content">
                                        <h4><?php echo get_the_author_meta( 'display_name' ); ?></h4>
                                        <p> <?php echo get_the_author_meta( 'description' ); ?>
                                        </p>
                                        <div class="social-media">
                                            <ul class="list-inline">
                                              <?php 
                                              $user_id = get_the_author_meta('ID'); 
                                              $social_media_fields = get_user_meta( $user_id, 'social_media_fields', true );

                                              

                                              if (is_array($social_media_fields)) {

                                                foreach ($social_media_fields as $platform => $url) {

                                                  $url = esc_url($url);

                                                  if (!empty($url)) {
                                                    $platform_icon_class = 'fab fa-'.$platform;  ?>

                                                    <li>
                                                    <a href="<?php echo $url; ?>">
                                                        <i class="<?php echo $platform_icon_class; ?>"></i>
                                                    </a>
                                                    </li>

                                                    <?php 
                                                    
                                                  }
                                                  
                                                }
                                                
                                              }

                                              ?>
                                                
                                                
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                            </div>


  <?php 


}